package com.example.gybicycle.common.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DataUtil {
    /**
     * 历时
     * @return xx天xx时xx分xx秒
     */
    public static String Duration(Date startTime,Date endTime){
        Calendar start = Calendar.getInstance();
        start.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        // 计算时间间隔（毫秒）
        long milliseconds = endTime.getTime()-startTime.getTime();
        long seconds=milliseconds/1000+(milliseconds%1000==0?0:1);
//        long days=seconds/(60*60*24);
        long hours=seconds/(60*60);
        long minutes=seconds%(60*60)/60;
        seconds=seconds%60;
        String date="";
//        if(days!=0)
//            date+=days+":";
        if(hours!=0)
            date+=hours+":";
        if(minutes!=0)
            date+=minutes+":";
        date+=seconds;
        return date;
    }
    public static List<Integer> DurationTimes(Date startTime, Date endTime){
        Calendar start = Calendar.getInstance();
        start.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        // 计算时间间隔（毫秒）
        long milliseconds = endTime.getTime()-startTime.getTime();
        long seconds=milliseconds/1000+(milliseconds%1000==0?0:1);
//        long days=seconds/(60*60*24);
        int hours=(int)(seconds/(60*60));
        int minutes=(int)(seconds%(60*60)/60);
      int miao=(int)(seconds%60);
        List<Integer> times=new ArrayList<>();
        times.add(hours);
        times.add(minutes);
        times.add(miao);
        return times;

    }


    public static int toInt(BigDecimal num){
        if(num==null)
            num=new BigDecimal(0);
        num=num.multiply(new BigDecimal(100)).setScale(0, RoundingMode.HALF_UP);
        return num.intValue();
    }
    /**
     * 历时 -秒
     * @param startTime
     * @param endTime
     * @return
     */
    public static  long DurationSecond(Date startTime,Date endTime){
        Calendar start = Calendar.getInstance();
        start.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        // 计算时间间隔（毫秒）
        long milliseconds = endTime.getTime()-startTime.getTime();
        long seconds=milliseconds/1000+(milliseconds%1000==0?0:1);
        return seconds;
    }
    /**
     * 历时 -分钟
     * @param startTime
     * @param endTime
     * @return
     */
    public static  long DurationMinutes(Date startTime,Date endTime){
        Calendar start = Calendar.getInstance();
        start.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        // 计算时间间隔（毫秒）
        long milliseconds = endTime.getTime()-startTime.getTime();
        long seconds=milliseconds/(1000*60)+(milliseconds%(1000*60)==0?0:1);
        return seconds;
    }
    /**
     * 历时 -小时
     * @param startTime
     * @param endTime
     * @return
     */
    public static  int DurationHour(Date startTime,Date endTime){
        Calendar start = Calendar.getInstance();
        start.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        // 计算时间间隔（毫秒）
        long milliseconds = endTime.getTime()-startTime.getTime();
        int seconds=(int)(milliseconds/(1000*60*60)+(milliseconds%(1000*60*60)==0?0:1));
        return seconds;
    }
    /**
     * 历时 -30分钟
     * @param startTime
     * @param endTime
     * @return
     */
    public static  int DurationHalfHour(Date startTime,Date endTime){
        Calendar start = Calendar.getInstance();
        start.setTime(startTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        // 计算时间间隔（毫秒）
        long milliseconds = endTime.getTime()-startTime.getTime();
        int seconds=(int)(milliseconds/(1000*60*30)+(milliseconds%(1000*60*30)==0?0:1));
        return seconds;
    }


    public static String setFileName(String fileName,Integer num){
        if(fileName==null)
            fileName="";
        int index=fileName.lastIndexOf(".");
        if(index>=0)
            fileName=fileName.substring(index + 1);
        else fileName="";
        fileName=DataUtil.toString(new Date(),"yyyyMMdd")+num+"."+fileName;
        return fileName;
    }
    public static String toString(Date str, String format){
        if(str==null)
            return null;
        SimpleDateFormat s=new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        String date=s.format(str);
        return date;
    }
    public static Date toDate(String date,String fmat){
        if(date==null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(fmat);
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(date, pos);
        return strtodate;
    }
    public static Date toDate(String date){
        if(date==null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(date, pos);
        return strtodate;
    }
    public static long toTomorrowScond(){
        Date date=new Date();
        long thisdate=date.getTime()/1000;
        long tomrrow=tomrrow();
        return tomrrow-thisdate;
    }
    public static  long tomrrow(){
        Calendar calendar=Calendar.getInstance();calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,8);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        Date date=calendar.getTime();
        return  date.getTime()/1000;
    }
}
